﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OA.Exam
{
    public partial class SetQuestionItems : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string qId = Request["qId"].ToString();

                HiddenQid.Value = qId;//题目编号
                string questionType = myExam.GetRecordOfQuestion(qId).Rows[0]["ItemType"].ToString();
                HiddenType.Value = questionType;//题目类型

                LabSubject.Text = myExam.GetRecordOfQuestion(qId).Rows[0]["Subject"].ToString();//题干

                switch (questionType)
                {
                    case "00001":
                        DivJudge.Visible = true;
                        DivItemContent.Visible = false;
                        DivAnswerEx.Visible = true;
                        DivItemList.Visible = false;
                        break;
                    case "00004":
                    case "00005":
                        DivJudge.Visible = false;
                        DivItemContent.Visible = true;
                        DivAnswerEx.Visible = true;
                        DivItemList.Visible = false;
                        break;
                    case "00002":
                    case "00003":
                        DivJudge.Visible = false;
                        DivItemContent.Visible = true;
                        DivAnswerEx.Visible = false;
                        DivItemList.Visible = true;
                        BtnSet.Visible = true;
                        //绑定题目选项列表
                        BindItems(qId);
                        break;
                    default:
                        DivJudge.Visible = false;
                        DivItemContent.Visible = false;
                        DivAnswerEx.Visible = false;
                        DivItemList.Visible = false;
                        break;
                }
                //编辑状态
                if (Request["qId"] != null)
                {
                    if (questionType == "00001")
                    {
                        RabJudge.SelectedValue = myExam.GetRecordOfQuestion(qId).Rows[0]["answer"].ToString();
                    }
                    if (questionType == "00004" || questionType == "00005")
                    {
                        contentItem.Value = myExam.GetRecordOfQuestion(qId).Rows[0]["answer"].ToString();
                    }
                    TxtAnswerEx.Text = myExam.GetRecordOfQuestion(qId).Rows[0]["answerex"].ToString();
                }
            }
        }

        protected void BindItems(string qId)
        {
            GridViewItemList.DataSource = myExam.GetItemListOfQuestion(qId);
            GridViewItemList.DataBind();
        }


        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string qId = HiddenQid.Value;
            string questionType = HiddenType.Value;

            string answer = string.Empty;
            string answerEx = TxtAnswerEx.Text.Trim();
            string itemNum = string.Empty;

            if (questionType == "00001")
            {
                answer = RabJudge.SelectedValue;
            }
            else
            {
                answer = contentItem.Value;
            }

            if (questionType == "00001" || questionType == "00004" || questionType == "00005")
            {
                if (myExam.InsertAnswerNoItem(qId,answer,answerEx))
                {
                    LabPrompt.Text = "保存成功";
                }
                else
                {
                    LabPrompt.Text = "保存失败";
                }
            }
            if (questionType == "00002" || questionType == "00003")
            {
                if (HiddenNum.Value == "")//新增选项
                {
                    itemNum = myExam.GetItemNumsOfQuestion(qId);
                    if (myExam.InsertQuestionItems(qId, itemNum, answer))
                    {
                        LabPrompt.Text = "保存成功";
                        contentItem.Value = "";
                        BindItems(qId);
                    }
                    else
                    {
                        LabPrompt.Text = "保存失败";
                    }
                }
                else//修改选项
                {
                    itemNum = HiddenNum.Value;
                    if (myExam.UpdateQuestionItems(qId, itemNum, answer))
                    {
                        LabPrompt.Text = "修改成功";
                        BindItems(qId);
                    }
                    else
                    {
                        LabPrompt.Text = "修改失败";
                    }
                }
            }
        }

        protected void BtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuestionsManage.aspx");
        }

        protected void GridViewItemList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string qId = HiddenQid.Value;
            string itemNum = e.CommandArgument.ToString();
            if (e.CommandName == "DeleteItem")
            {
                
                if (myExam.DeleteItemOfQuestion(qId, itemNum))
                {
                    LabPrompt.Text = "删除成功";
                    BindItems(qId);
                }
                else
                {
                    LabPrompt.Text = "删除失败";
                }
            }
            if (e.CommandName == "EditItem")
            {
                LabPrompt.Text = "";
                HiddenNum.Value = itemNum;
                contentItem.Value = myExam.GetContentOfItem(qId, itemNum);
            }
        }
        //弹出框
        protected void BtnSet_Click(object sender, EventArgs e)
        {
            ModalPopupExtender1.Show();
            if (HiddenType.Value == "00002")
            {
                DivRadio.Visible = true;
                DataListRadio.DataSource = myExam.GetItemListOfQuestion(HiddenQid.Value);
                DataListRadio.DataBind();
            }
            else
            {
                DivMultiple.Visible = true;
                DataListMultiple.DataSource = myExam.GetItemListOfQuestion(HiddenQid.Value);
                DataListMultiple.DataBind();
            }
        }
        //设置选择题答案和解析
        protected void BtnOk_Click(object sender, EventArgs e)
        {
            int i;
            bool flag = false;
            string answer = string.Empty;
            string answerEx = TxtCheckAnswerEx.Text.Trim();
            if (HiddenType.Value == "00002")
            {
                for (i = 0; i < DataListRadio.Items.Count; i++)
                {
                    RadioButton RabItem = (RadioButton)DataListRadio.Items[i].FindControl("RabItem");
                    Label LabRadioItem = (Label)DataListRadio.Items[i].FindControl("LabRadioItem");
                    if (RabItem.Checked)
                    {
                        answer = LabRadioItem.Text.Trim();
                        flag = myExam.SetCheckQuestionAnswer(HiddenQid.Value, answer,answerEx);
                    }
                }
            }
            else
            {
                for (i = 0; i < DataListMultiple.Items.Count; i++)
                {
                    CheckBox ChbItem = (CheckBox)DataListMultiple.Items[i].FindControl("ChbItem");
                    Label LabMultipleItem = (Label)DataListMultiple.Items[i].FindControl("LabMultipleItem");
                    if (ChbItem.Checked)
                    {
                        answer += LabMultipleItem.Text.Trim() + ",";
                    }
                }
                if (answer.IndexOf(',') != -1)
                {
                    answer = answer.TrimEnd(',');
                }
                flag = myExam.SetCheckQuestionAnswer(HiddenQid.Value, answer,answerEx);
            }
            if (flag)
            {
                LabPrompt.Text = "设置成功";
            }
            else
            {
                LabPrompt.Text = "设置失败请重试";
            }
        }
        //绑定单选题已设定答案
        protected void DataListRadio_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (((Label)e.Item.FindControl("LabRadioItem")).Text == myExam.GetRecordOfQuestion(HiddenQid.Value).Rows[0]["answer"].ToString())
                {
                    ((RadioButton)e.Item.FindControl("RabItem")).Checked = true;
                }
            }
        }
        //绑定多选题已设定答案
        protected void DataListMultiple_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            string answer = myExam.GetRecordOfQuestion(HiddenQid.Value).Rows[0]["answer"].ToString();
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if(answer.IndexOf(',')==-1)
                {
                    if (((Label)e.Item.FindControl("LabMultipleItem")).Text == answer)
                    {
                        ((CheckBox)e.Item.FindControl("ChbItem")).Checked = true;
                    }
                }
                else{
                    string[] arr = answer.Split(',');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (((Label)e.Item.FindControl("LabMultipleItem")).Text == arr[i])
                        {
                            ((CheckBox)e.Item.FindControl("ChbItem")).Checked = true;
                        }
                    }
                }
            }
        }
    }
}
